<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <!-- <el-form-item label="逻辑删除" prop="isDelete">
        <el-input
          v-model="queryParams.isDelete"
          placeholder="请输入逻辑删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="A表名" prop="tableaName">
        <el-input
          v-model="queryParams.tableaName"
          placeholder="请输入A表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字典表名" prop="tablebName">
        <el-input
          v-model="queryParams.tablebName"
          placeholder="请输入字典表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="查询K值" prop="fieldA">
        <el-input
          v-model="queryParams.fieldA"
          placeholder="请输入查询K值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字典主键K值" prop="fieldB">
        <el-input
          v-model="queryParams.fieldB"
          placeholder="请输入字典主键K值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="返回结果V值" prop="resultField">
        <el-input
          v-model="queryParams.resultField"
          placeholder="请输入返回结果V值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作符" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入操作符"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="字典描述" prop="relationshipDescription">
        <el-input
          v-model="queryParams.relationshipDescription"
          placeholder="请输入字典描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="是否代码块" prop="isCodeBlock">
        <el-input
          v-model="queryParams.isCodeBlock"
          placeholder="请输入是否代码块"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['conf:CalulatorTableRelation:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['conf:CalulatorTableRelation:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['conf:CalulatorTableRelation:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['conf:CalulatorTableRelation:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="CalulatorTableRelationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="逻辑删除" align="center" prop="isDelete" />
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="A表名" align="center" prop="tableaNameComment" />
      <el-table-column label="查询K值" align="center" prop="fieldAComment">
        <template v-slot="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="opentagkey(scope.row, 'A')"
            >{{ scope.row.fieldAComment }}</span
          >
        </template></el-table-column
      >
      <!--  -->
      <el-table-column label="操作符" align="center" prop="operator" />
      <el-table-column
        label="字典表名"
        align="center"
        prop="tablebNameComment"
      />

      <el-table-column label="字典主键K值" align="center" prop="fieldBComment">
        <template v-slot="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="opentagkey(scope.row, 'B')"
            >{{ scope.row.fieldBComment }}</span
          >
        </template></el-table-column
      >
      <!--  -->
      <el-table-column
        label="返回结果V值"
        align="center"
        prop="resultFieldComment"
      />

      <el-table-column
        label="字典描述"
        align="center"
        prop="relationshipDescription"
      />
      <el-table-column label="字典条件" align="center">
        <template slot-scope="scope">
          <span
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="tableEdit(scope.row)"
            >字典条件</span
          >
        </template>
      </el-table-column>
      <el-table-column label="是否代码块" align="center" prop="isCodeBlock" />
      <el-table-column label="代码块" align="center" prop="codeBlock" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['conf:CalulatorTableRelation:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['conf:CalulatorTableRelation:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改打标关系计算对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="逻辑删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入逻辑删除" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
        <el-form-item label="A表名" prop="connectionTableName">
          <!-- <el-input
            v-model="form.connectionTableName"
            placeholder="请输入连接表名"
          /> -->
          <el-select
            v-model="form.connectionTableName"
            clearable
            filterable
            remote
            :remote-method="getDatacomment"
            placeholder="请输入A表名"
            allow-create
            @change="changeconnectionTableName"
          >
            <el-option
              v-for="item in tablecomment"
              :key="item.tableName"
              :label="`${item.comment}`"
              :value="item.tableName"
            >
            </el-option>
          </el-select>
          <el-select
            v-model="form.connectionTableName"
            clearable
            filterable
            remote
            :remote-method="gettableName"
            placeholder="数据库A表名"
            allow-create
            @change="changeconnectionTableName"
          >
            <el-option
              v-for="item in tablecomment"
              :key="item.tableName"
              :label="`${item.tableName}`"
              :value="item.tableName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="查询K值" prop="connectionConditionField">
          <el-select
            v-model="form.fieldAList"
            clearable
            filterable
            placeholder="请输入查询K值"
            @focus="selectconnectionConditionFields(form.connectionTableName)"
            multiple
          >
            <el-option
              v-for="item in connectionConditionFields"
              :key="item.columnName"
              :label="`${item.comment}`"
              :value="item.columnName"
            >
            </el-option>
          </el-select>
          <!-- <el-input
            v-model="form.connectionConditionField"
            placeholder="请输入连接条件表字段"
          /> -->
        </el-form-item>
        <el-form-item label="操作符" prop="operator">
          <!-- <el-input v-model="form.operatorValue" placeholder="请输入操作符" /> -->
          <el-select v-model="form.operator" placeholder="请输入操作符">
            <el-option label="=" value="=" />
            <el-option label="!=" value="!=" />
            <el-option label=">" value=">" />
            <el-option label=">=" value=">=" />
            <el-option label="<" value="<" />
            <el-option label="<=" value="<=" />
            <!-- <el-option label="LIKE" value="LIKE" />
            <el-option label="BETWEEN" value="BETWEEN" /> -->
          </el-select>
        </el-form-item>
        <el-form-item label="字典表名" prop="mainTableName">
          <!-- <el-input v-model="form.mainTableName" placeholder="请输入主表名" /> -->
          <el-select
            v-model="form.mainTableName"
            clearable
            filterable
            remote
            :remote-method="getDatacomment"
            placeholder="请输入字典表名"
            allow-create
            @change="changemainTableName"
          >
            <el-option
              v-for="item in tablecomment"
              :key="item.tableName"
              :label="`${item.comment}`"
              :value="item.tableName"
            >
            </el-option>
          </el-select>

          <el-select
            v-model="form.mainTableName"
            clearable
            filterable
            remote
            :remote-method="gettableName"
            placeholder="数据库字典表名"
            allow-create
            @change="changemainTableName"
          >
            <el-option
              v-for="item in tablecomment"
              :key="item.tableName"
              :label="`${item.tableName}`"
              :value="item.tableName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="B表字段" prop="mainTableField">
          <!-- <el-input
            v-model="form.mainTableField"
            placeholder="请输入主表字段"
          /> -->
          <el-select
            v-model="form.fieldBList"
            clearable
            filterable
            placeholder="B表字段"
            @focus="selectmainTableField(form.mainTableName)"
            multiple
          >
            <el-option
              v-for="item in mainTableFields"
              :key="item.columnName"
              :label="`${item.comment}`"
              :value="item.columnName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="返回结果V值" prop="resultField">
          <!-- <el-input
            v-model="form.mainTableField"
            placeholder="请输入主表字段"
          /> 
        
        -->
          <el-select
            v-model="form.resultFieldList"
            clearable
            filterable
            placeholder="返回结果V值"
            @focus="selectmainTableField(form.mainTableName)"
            multiple
          >
            <el-option
              v-for="item in mainTableFields"
              :key="item.columnName"
              :label="`${item.comment}`"
              :value="item.columnName"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="A表名" prop="tableaName">
          <el-input v-model="form.tableaName" placeholder="请输入A表名" />
        </el-form-item>
        <el-form-item label="字典表名" prop="tablebName">
          <el-input v-model="form.tablebName" placeholder="请输入字典表名" />
        </el-form-item>
        <el-form-item label="查询K值" prop="fieldA">
          <el-input v-model="form.fieldA" placeholder="请输入查询K值" />
        </el-form-item>
        <el-form-item label="字典主键K值" prop="fieldB">
          <el-input v-model="form.fieldB" placeholder="请输入字典主键K值" />
        </el-form-item>
        <el-form-item label="返回结果V值" prop="resultField">
          <el-input v-model="form.resultField" placeholder="请输入返回结果V值" />
        </el-form-item>
        <el-form-item label="操作符" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入操作符" />
        </el-form-item> -->
        <el-form-item label="字典描述" prop="relationshipDescription">
          <el-input
            v-model="form.relationshipDescription"
            placeholder="请输入字典描述"
          />
        </el-form-item>
        <!-- <el-form-item label="是否代码块" prop="isCodeBlock">
          <el-input v-model="form.isCodeBlock" placeholder="请输入是否代码块" />
        </el-form-item>
        <el-form-item label="代码块" prop="codeBlock">
          <el-input
            v-model="form.codeBlock"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="tagkeyVisible"
      width="800px"
      append-to-body
      v-if="tagkeyVisible"
      title="字段比较前规整规则"
    >
      <div class="el-dialog-div">
        <el-form :model="formData" :inline="true">
          <!-- <el-form-item label="采购单号:">
              {{ this.list[0].purchaseOrderNumber }}
            </el-form-item>
            <el-form-item label="sku:">
              {{ this.list[0].sku }}
            </el-form-item> -->
          <el-card>
            <el-form-item label="条件关系">
              <!-- <el-input v-model="formData.libraryIdList[0].conditionRelation" placeholder="条件关系" /> -->
              <el-select
                v-model="formData.libraryIdList[0].conditionRelation"
                clearable
                filterable
                placeholder="条件关系"
                style="width: 200px"
              >
                <el-option
                  v-for="(item, i) in conditionRelationList"
                  :key="i"
                  :label="`${item}`"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-card>
          <el-card>
            <el-row>
              <el-form-item style="width: 200px" label="条件"></el-form-item>
              <el-form-item style="width: 150px" label="条件值"></el-form-item>
            </el-row>
            <el-form-item
              v-for="(item, index) in formData.libraryIdList"
              :key="index"
            >
              <!--
                包含/以XX结尾/以XX开头/长度等于/包含数字/纯字母
                <el-input
              style="width: 150px"
              v-model="item.storeName"
              placeholder="店铺名称"
            ></el-input> -->
              <el-select
                v-model="item.conditionType"
                clearable
                filterable
                placeholder="条件"
                style="width: 200px"
              >
                <el-option
                  v-for="(item, i) in ShopInfoList"
                  :key="i"
                  :label="`${item}`"
                  :value="item"
                >
                </el-option>
              </el-select>
              <!-- <el-input
              style="width: 150px"
              v-model="item.field_name"
              placeholder="请输入字段值"
            ></el-input> -->
              <el-input
                style="width: 150px"
                v-model="item.conditionValue"
                placeholder="条件值"
              ></el-input>

              <span
                v-if="formData.libraryIdList.length > 0"
                style="margin-left: 5px"
              >
                <el-button type="primary" size="mini" @click="addLibraryIdList"
                  >新增</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  @click="delRow(item, index)"
                  >删除</el-button
                >
              </span>
            </el-form-item>
          </el-card>
          <el-card>
            <el-form-item label="行动">
              <!-- <el-input v-model="strategyCode" placeholder="行动" /> -->
              <el-select
                v-model="formData.libraryIdList[0].action"
                clearable
                filterable
                placeholder="行动"
                style="width: 200px"
              >
                <el-option
                  v-for="(item, i) in actionList"
                  :key="i"
                  :label="`${item}`"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="行动值">
              <el-input
                v-model="formData.libraryIdList[0].actionValue"
                placeholder="行动值"
              />
            </el-form-item>
          </el-card>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleclearData">清 空</el-button>
        <el-button type="primary" @click="submitformData">确 定</el-button>
        <el-button @click="canceltagkeyVisible">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="openRule"
      width="90%"
      append-to-body
      v-if="openRule"
    >
      <BiRuleSpecifications :row="row" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listFieldrule,
  getFieldrule,
  delFieldrule,
  addFieldrule,
  updateFieldrule,
  insertData,
  clearData,
} from "@/api/conf/fieldrule";
import {
  listCalulatorTableRelation,
  getCalulatorTableRelation,
  delCalulatorTableRelation,
  addCalulatorTableRelation,
  updateCalulatorTableRelation,
} from "@/api/conf/CalulatorTableRelation";
import {
  selectTableFields,
  selectTableNameList,
} from "@/api/conf/SysTableField";
import BiRuleSpecifications from "@/views/conf/BiRuleSpecifications/relationindex.vue";
export default {
  name: "CalulatorTableRelation",
  components: {
    BiRuleSpecifications,
  },
  props: {
    // applicationTable: {
    //   type: String,
    //   default: "",
    // },
    querytagName: {
      type: String,
      default: null,
    },
  },

  data() {
    return {
      openRule: false,
      row: {},
      conditionRelationList: ["AND", "OR"],
      actionList: ["删除", "替换", "大写", "小写", "去首尾空格"],
      fieldType: "",
      ShopInfoList: [
        "包含",
        "以XX结尾",
        "以XX开头",
        "长度等于",
        "包含数字",
        "纯字母",
      ],
      rowInfo: {},
      tagkeyVisible: false,
      formData: {
        libraryIdList: [
          {
            conditionType: "",
            conditionValue: "",
            action: "",
            actionValue: "",
            conditionRelation: "",
          },
        ],
      },

      mainTableFields: [],
      connectionConditionFields: [],
      tablecomment: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 打标关系计算表格数据
      CalulatorTableRelationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isDelete: null,
        tableaName: null,
        tablebName: null,
        fieldA: null,
        fieldB: null,
        resultField: null,
        operator: null,
        relationshipDescription: null,
        isCodeBlock: null,
        codeBlock: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  watch: {
    applicationTable: {
      handler(newVal, oldVal) {
        this.queryParams.relationshipDescription = this.querytagName;
        this.getList();
        // this.selectgetTablefieldsWithComments();
      },
      deep: true,
      immediate: true,
    },
  },

  methods: {
    tableEdit(row, column, cell, event) {
      this.row = row;
      this.openRule = true;
    },
    opentagkey(row, fieldType) {
      this.fieldType = fieldType;
      // listShop({
      //   strategyCode: this.row.strategyCode,
      //   businessType: "priority",
      // }).then((response) => {
      //   let list = [{ storeName: "", plannedAllocationQty: "" }];
      //   if (response.rows.length > 0) {
      //     list = [];
      //     response.rows.forEach((item) => {
      //       list.push({
      //         storeName: item.storeName,
      //         plannedAllocationQty: item.plannedAllocationQty,
      //       });
      //     });
      //   }
      //   // this.cols
      //   this.formData.libraryIdList = list;
      // });
      // if (this.list.length != 1) {
      //   this.$modal.msgError("只勾选一条记录");
      //   return;
      // }

      let obj = {
        pageNum: 1,
        pageSize: 100,
        tableRelationshipId: row.id,
        fieldType: fieldType,
      };
      listFieldrule(obj).then((response) => {
        if (response.rows.length > 0) {
          this.formData.libraryIdList = [];
          response.rows.forEach((item) => {
            this.formData.libraryIdList.push({ ...item });
          });
        } else {
          this.formData.libraryIdList = [
            {
              conditionType: "",
              conditionValue: "",
            },
          ];
        }
      });
      // if (!row.sqlTableName) {
      //   return;
      // }
      // getTablefieldsWithComments({
      //   tableName: row.sqlTableName,
      // }).then((response) => {
      //   this.tableNameList = [];
      //   this.tableNameList = response;
      // });
      this.rowInfo = row;
      this.tagkeyVisible = true;
    },
    submitformData() {
      // const hasEmptyProductNo = this.formData.libraryIdList.some(
      //   (product) => product.fieldName == ""
      // );
      // if (hasEmptyProductNo) {
      //   this.$modal.msgError("检查字段是否为空");
      //   return;
      // }
      this.formData.libraryIdList.forEach((item) => {
        item.tableRelationshipId = this.rowInfo.id;
        item.fieldType = this.fieldType;
        item.action = this.formData.libraryIdList[0].action;
        item.actionValue = this.formData.libraryIdList[0].actionValue;
        item.conditionRelation =
          this.formData.libraryIdList[0].conditionRelation;
        // item.strategyCode = this.strategyCode;
      });
      insertData(this.formData.libraryIdList).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess("新增成功");
          this.tagkeyVisible = false;
          this.getList();
        }
      });
    },
    handleclearData() {
      this.formData = {
        libraryIdList: [
          {
            tableRelationshipId: this.rowInfo.id,
            fieldType: this.fieldType,
            conditionType: "",
            conditionValue: "",
            action: "",
            actionValue: "",
            conditionRelation: "",
          },
        ],
      };

      clearData({
        tableRelationshipId: this.rowInfo.id,
        fieldType: this.fieldType,
        conditionType: "",
        conditionValue: "",
        action: "",
        actionValue: "",
        conditionRelation: "",
      }).then((response) => {
        this.$modal.msgSuccess("已清空");
      });
    },
    // 取消按钮
    canceltagkeyVisible() {
      this.tagkeyVisible = false;
    },
    //添加库存名称
    addLibraryIdList() {
      this.formData.libraryIdList.push({
        conditionType: "",
        conditionValue: "",
      });
    },
    //库存名称删除
    delRow(item, index) {
      if (this.formData.libraryIdList.length > 0) {
        this.formData.libraryIdList.splice(index, 1);
        // if (!item.isEdit) {

        // } else {
        //   const ids = item.id;
        //   this.$modal
        //     .confirm('是否确认删除删除更新条件编号为"' + ids + '"的数据项？')
        //     .then(function () {
        //       return delBiDeleteCondition(ids);
        //     })
        //     .then(() => {
        //       // this.getList();
        //       this.$modal.msgSuccess("删除成功");
        //     })
        //     .catch(() => {});
        // }
      } else {
        this.$message.warning("至少选择一项");
      }
    },

    changeresultField(value) {
      const selectedOption = this.mainTableFields.find(
        (option) => option.columnName === value
      );
      console.log(JSON.stringify(selectedOption)); // 这里就是整个选项对象
      this.form.resultFieldComment = selectedOption.comment;
    },
    changemainfieldName(value) {
      const selectedOption = this.mainTableFields.find(
        (option) => option.columnName === value
      );
      console.log(JSON.stringify(selectedOption)); // 这里就是整个选项对象
      this.form.fieldBComment = selectedOption.comment;
    },

    selectmainTableField(tableName) {
      if (!tableName) {
        return;
      }
      selectTableFields({ tableName: tableName }).then((response) => {
        this.mainTableFields = response.map((item) => ({
          columnName: item.columnName,
          comment: item.comment ? item.comment : item.columnName,
        }));
      });
    },
    changeconnectionfieldName(value) {
      const selectedOption = this.connectionConditionFields.find(
        (option) => option.columnName === value
      );
      console.log(JSON.stringify(selectedOption)); // 这里就是整个选项对象
      this.form.fieldAComment = selectedOption.comment;
    },
    selectconnectionConditionFields(tableName) {
      if (!tableName) {
        return;
      }
      selectTableFields({ tableName: tableName }).then((response) => {
        this.connectionConditionFields = response.map((item) => ({
          columnName: item.columnName,
          comment: item.comment ? item.comment : item.columnName,
        }));
      });
    },
    changeconnectionTableName(value) {
      const selectedOption = this.tablecomment.find(
        (option) => option.tableName === value
      );
      console.log(JSON.stringify(selectedOption)); // 这里就是整个选项对象
      this.form.tableaNameComment = selectedOption.comment;
    },
    changemainTableName(value) {
      const selectedOption = this.tablecomment.find(
        (option) => option.tableName === value
      );
      console.log(JSON.stringify(selectedOption)); // 这里就是整个选项对象
      this.form.tablebNameComment = selectedOption.comment;
    },
    getDatacomment(key) {
      selectTableNameList({ comment: key }).then((response) => {
        this.tablecomment = response;
      });
    },
    gettableName(key) {
      selectTableNameList({ tableName: key }).then((response) => {
        this.tablecomment = response;
      });
    },
    /** 查询打标关系计算列表 */
    getList() {
      this.loading = true;
      listCalulatorTableRelation(this.queryParams).then((response) => {
        this.CalulatorTableRelationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        remark: null,
        tableaName: null,
        tablebName: null,
        fieldA: null,
        fieldB: null,
        resultField: null,
        operator: null,
        relationshipDescription: null,
        isCodeBlock: null,
        codeBlock: null,
        resultFieldList: [],
        mainTableField: [],
        connectionConditionField: [],
        fieldAList: [],
        fieldBList: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      this.open = true;
      this.title = "添加打标关系计算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCalulatorTableRelation(id).then((response) => {
        this.form = response.data;
        //映射字段
        this.form.connectionTableName = this.form.tableaName;
        // this.form.connectionConditionField = this.form.fieldA;
        this.form.mainTableName = this.form.tablebName;
        // this.form.mainTableField = this.form.fieldB;
        this.form.fieldAList = this.form.fieldA.split("/");
        this.form.fieldBList = this.form.fieldB.split("/");
        this.form.resultFieldList = this.form.resultField.split("/");
        //映射查询
        this.getDatacomment("");
        this.selectconnectionConditionFields(this.form.connectionTableName);
        this.selectmainTableField(this.form.mainTableName);
        this.open = true;
        this.title = "修改打标关系计算";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.form.fieldAList) {
        this.$modal.msgError("查询K值不允许为空");
        return;
      }
      if (!this.form.fieldBList) {
        this.$modal.msgError("B表字段不允许为空");
        return;
      }
      if (!this.form.resultFieldList) {
        this.$modal.msgError("返回结果V值不允许为空");
        return;
      }
      this.form.tableaName = this.form.connectionTableName;
      // this.form.fieldAList = this.form.connectionConditionField;
      if (this.form.fieldAList) {
        this.form.fieldACommentList = this.form.fieldAList.map((item) => {
          const match = this.connectionConditionFields.find(
            (obj) => obj.columnName === item
          );
          return match ? match.comment : null; // 如果没有找到匹配项，返回null
        });
      }

      this.form.tablebName = this.form.mainTableName;
      // this.form.fieldBList = this.form.mainTableField;
      if (this.form.fieldBList) {
        this.form.fieldBCommentList = this.form.fieldBList.map((item) => {
          const match = this.mainTableFields.find(
            (obj) => obj.columnName === item
          );
          return match ? match.comment : null; // 如果没有找到匹配项，返回null
        });
      }
      if (this.form.resultFieldList) {
        this.form.resultFieldCommentList = this.form.resultFieldList.map(
          (item) => {
            const match = this.mainTableFields.find(
              (obj) => obj.columnName === item
            );
            return match ? match.comment : null; // 如果没有找到匹配项，返回null
          }
        );
      }

      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCalulatorTableRelation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCalulatorTableRelation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除打标关系计算编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCalulatorTableRelation(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "conf/CalulatorTableRelation/export",
        {
          ...this.queryParams,
        },
        `CalulatorTableRelation_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
